

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using SubSonic;

namespace JTC.MiniCommerce.Store {
  public partial class PageController {

    #region Constants

    private const string MENUS = "Menus";
    private const string MENU = "Menu";
    private const string PARENT_CHILD = "ParentChild";
    private const string ASC = "ASC";
    private const string PAGE_ID = "PageId";
    private const string PARENT_ID = "ParentId";

    #endregion

    #region Methods
    
    #region Public
    
    /// <summary>
    /// Fetches the page list.
    /// </summary>
    /// <returns></returns>
    public DataSet FetchPageList() {
      DataSet ds = new Query(Store.Page.Schema).ORDER_BY(Store.Page.Columns.SortOrder, ASC).ExecuteDataSet();
      ds.DataSetName = MENUS;
      ds.Tables[0].TableName = MENU;
      DataRelation relation = new DataRelation(PARENT_CHILD, ds.Tables[MENU].Columns[PAGE_ID], ds.Tables[MENU].Columns[PARENT_ID], false);
      relation.Nested = true;
      ds.Relations.Add(relation);
      return ds;
    }

    /// <summary>
    /// Fetches the default page.
    /// </summary>
    /// <returns></returns>
    public Page FetchDefaultPage() {
      Query query = new Query(Page.Schema).AddWhere(Page.Columns.ParentId, Comparison.Equals, 0).AddWhere(Page.Columns.SortOrder, Comparison.Equals, 1);
      PageCollection pageCollection = new PageController().FetchByQuery(query);
      Page page = null;
      if (pageCollection.Count > 0) {
        page = pageCollection[0];
      }
      return page;
    }
    
    #endregion
    
    #endregion
    
  }
}
